package com.example.thirdhomework

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import androidx.activity.enableEdgeToEdge
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import com.example.thirdhomework.databinding.ActivityMainBinding

//第一个消息传给第二个
const val KEY1="key1"
const val KEY2="key2"
class MainActivity : AppCompatActivity() {
    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
//Second Activity
        binding.first.setOnClickListener {

            // 使用意图intent进行跳转页面 this当前的页面main
            val intent = Intent(this, SecondActivity::class.java)
            //intent传参 启动意图 第二个页面可以取到getIntent 正向传参
            intent.putExtra(KEY1, binding.enditext.text.toString())
            startActivity(intent)
        }
//Third Activity
        // 从第二个页面传数据
        //（反向传参startActivityForResult requestCode:请求码（1-》2 2-》1防止activity乱了））
        // 监听和响应
        binding.second.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivityForResult(intent, 0)
        }
//Forth Acitivity
        binding.third.setOnClickListener{
            val intent = Intent(this,ForthActivity::class.java)
           startActivity(intent)
        }
//Fifth Activity
        binding.forth.setOnClickListener{
            val intent = Intent(this,FifthActivity::class.java)
            startActivity(intent)
        }
    }

//Third Activity
        //拿回参数 回调
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {//？是可选是否输入
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == 0) {
                if (resultCode == 1) {//1是确认的意思
                    binding.textView.text = data?.getStringExtra(KEY2)//?是安全调用 如果第二个页面输入为空则不执行 安全调用
                }
            }
        }
}
